package com.marvels.financial.collection.controller;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.collection.dto.ReminderDTO;
import com.marvels.financial.collection.dto.UrgentDTO;
import com.marvels.financial.collection.service.CheckListQueryService;

/**
 * 清单查询
 * @author zhaosa
 *
 */
@RestController
@RequestMapping("financial/collection/checkListQuery/service")
public class CheckListQueryServiceController {
	
	@Autowired
	private CheckListQueryService checkListQueryService;
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	@RequestMapping("selectReminderList")
	@ResponseBody
	public MarvelsResultDTO selectReminderList(@RequestBody ReminderDTO reminderDTO){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			PageInfo<ReminderDTO> pageInfo = checkListQueryService.selectReminderListByPage(reminderDTO);
			String totalSum = checkListQueryService.selectReminderTotalSum(reminderDTO);
			result.put("list", pageInfo);
			result.put("totalSum", totalSum);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询入催列表FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询入催列表异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "入催列表查询异常", null);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), result);
	}
	
	@RequestMapping("/downLoadReminderExcel")
	public void excelDownload(ReminderDTO reminderDTO,HttpServletResponse response,HttpServletRequest request){
		BufferedOutputStream output = null;
		ByteArrayOutputStream byteArrayOutputStream =null;
		try {
			String fileName = "本日入催清单.xls";
			HSSFWorkbook wb = checkListQueryService.findExcelWorkbook(reminderDTO);
			byteArrayOutputStream = new ByteArrayOutputStream();
			wb.write(byteArrayOutputStream);
			byte[] bs = byteArrayOutputStream.toByteArray();
			byteArrayOutputStream.flush();
			byteArrayOutputStream.close();
			response.setContentType("application/octet-stream;charset=ISO8859-1");
			
			output = new BufferedOutputStream(response.getOutputStream());
			// 中文文件名必须转码为 ISO8859-1,否则为乱码
			String fileNameDown = new String(fileName.getBytes(), "ISO8859-1");
			// 作为附件下载
			response.setHeader("Content-Disposition", "attachment;filename=" + fileNameDown);
			
			output.write(bs);
			response.flushBuffer();
			output.close();
			
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>下载入催清单--查询列表异常:", e);
		}
		finally {
			if(byteArrayOutputStream!=null){
				try {
					byteArrayOutputStream.close();
				} catch (IOException e) {
					MarvelsLogUtil.getInstance().error(">>>>>>>>>关闭输出流异常byteArrayOutputStream", e);
				}
			}
			if (output != null)
				try {
					output.close();
				} catch (IOException e) {
					MarvelsLogUtil.getInstance().error(">>>>>>>>>关闭输出流异常output", e);
				}
		}
		
	}
	@RequestMapping("selectUrgentList")
	@ResponseBody
	public MarvelsResultDTO selectUrgentList(@RequestBody UrgentDTO urgentDTO){
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			PageInfo<UrgentDTO> pageInfo = checkListQueryService.selectUrgentList(urgentDTO);
			String totalSum = checkListQueryService.selectUrgentTotalSum(urgentDTO);
			result.put("list", pageInfo);
			result.put("totalSum", totalSum);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询出催列表FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		}catch(Exception e){
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询出催列表异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "出催列表查询异常", null);
		}
		return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(), result);
	}
	@RequestMapping("/downLoadUrgentExcel")
	public void excelUrgentDownload(UrgentDTO urgentDTO,HttpServletResponse response,HttpServletRequest request){
		BufferedOutputStream output = null;
		ByteArrayOutputStream byteArrayOutputStream=null;
		try {
			String fileName = "本日出催清单.xls";
			HSSFWorkbook wb = checkListQueryService.findUrgentExcelWorkbook(urgentDTO);
			byteArrayOutputStream = new ByteArrayOutputStream();
			wb.write(byteArrayOutputStream);
			byte[] bs = byteArrayOutputStream.toByteArray();
			byteArrayOutputStream.flush();
			byteArrayOutputStream.close();
			response.setContentType("application/octet-stream;charset=ISO8859-1");
			
			output = new BufferedOutputStream(response.getOutputStream());
			// 中文文件名必须转码为 ISO8859-1,否则为乱码
			String fileNameDown = new String(fileName.getBytes(), "ISO8859-1");
			// 作为附件下载
			response.setHeader("Content-Disposition", "attachment;filename=" + fileNameDown);
			
			output.write(bs);
			response.flushBuffer();
			output.close();
			
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>下载出催清单--查询列表异常:", e);
		}
		finally {
			if(byteArrayOutputStream!=null){
				try {
					byteArrayOutputStream.close();
				} catch (IOException e) {
					MarvelsLogUtil.getInstance().error(">>>>>>>>>关闭输出流异常byteArrayOutputStream", e);
				}
			}
			if (output != null)
				try {
					output.close();
				} catch (IOException e) {
					MarvelsLogUtil.getInstance().error(">>>>>>>>>关闭输出流异常output", e);
				}
		}
		
	}
	
	@ResponseBody
	@RequestMapping("findConditionList")
	public MarvelsResultDTO findConditionList(){
		try {
			Map<String, Object> map = checkListQueryService.findConditionList();
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					map);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>出入催查询产品list的筛选FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), MarvelsErrorCode.FAIL.getDesc(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>出入催查询产品list的筛选条件:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),
					null);
		}
	}
}
